package com.tepth.busfix.main.repair.complete;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.tepth.busfix.R;
import com.tepth.busfix.bean.reponse.Person;
import com.tepth.busfix.bean.reponse.Trouble;
import com.tepth.busfix.bean.reponse.TroubleContent;

import java.util.List;

/**
 * Created by zhangyu on 2017/7/17.
 */

public class OperatorAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<Trouble> mList;

    public OperatorAdapter(Context context, List<Trouble> list) {
        mContext = context;
        mList = list;
    }

    public List<Trouble> getDatas() {
        return mList;
    }

    @Override
    public int getGroupCount() {
        return mList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mList.get(groupPosition).getListContent().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mList.get(groupPosition).getListContent().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder groupHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_head_operator, null);
            groupHolder = new GroupHolder();
            groupHolder.txt = (TextView) convertView.findViewById(R.id.tv_trouble_name);
            groupHolder.cb = (CheckBox) convertView.findViewById(R.id.btn);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) convertView.getTag();
        }

        final Trouble trouble = mList.get(groupPosition);
        groupHolder.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                trouble.setChecked(!trouble.isChecked());
            }
        });
        if (trouble.isChecked()) {
            groupHolder.cb.setChecked(true);
        } else {
            groupHolder.cb.setChecked(false);
        }

        groupHolder.txt.setText(trouble.getName().replaceAll("自定义录入@@", "附加作业项"));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ItemHolder itemHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_repair_operator_work, null);
            itemHolder = new ItemHolder();
            itemHolder.cb = (CheckBox) convertView.findViewById(R.id.checkbox);
            itemHolder.tvTrouble = (TextView) convertView.findViewById(R.id.tv_trouble);
            itemHolder.tvOperator = (TextView) convertView.findViewById(R.id.tv_operator);
            itemHolder.tvPart = (TextView) convertView.findViewById(R.id.tv_part);
            itemHolder.ivMinus = (ImageView) convertView.findViewById(R.id.iv_minus);
            itemHolder.tvCount = (TextView) convertView.findViewById(R.id.tv_count);
            itemHolder.ivAdd = (ImageView) convertView.findViewById(R.id.iv_add);
            itemHolder.tvUnit = (TextView) convertView.findViewById(R.id.tv_unit);
            itemHolder.tvUnit = (TextView) convertView.findViewById(R.id.tv_unit);
            itemHolder.tvPerson = (TextView) convertView.findViewById(R.id.tv_person);
            itemHolder.btnDelete = (ImageView) convertView.findViewById(R.id.btn_delete);
            itemHolder.btnComplete = (ImageView) convertView.findViewById(R.id.iv_complete);

            convertView.setTag(itemHolder);
        } else {
            itemHolder = (ItemHolder) convertView.getTag();
        }
        TroubleContent troubleContent = mList.get(groupPosition).getListContent().get(childPosition);
        itemHolder.cb.setVisibility(View.INVISIBLE);
        itemHolder.ivMinus.setVisibility(View.INVISIBLE);
        itemHolder.ivAdd.setVisibility(View.INVISIBLE);
        itemHolder.tvTrouble.setVisibility(View.GONE);
        itemHolder.btnDelete.setVisibility(View.GONE);
        itemHolder.btnComplete.setVisibility(View.GONE);

        itemHolder.tvOperator.setText(troubleContent.getOperation().getName());
        TroubleContent.Part part = troubleContent.getAccessory() == null ? troubleContent.getComponent() : troubleContent.getAccessory();
        itemHolder.tvPart.setText(part.getName());
        itemHolder.tvCount.setText(troubleContent.getUseCount() + "");
        itemHolder.tvUnit.setText(part.getUnit());
        Person person = troubleContent.getWorkPerson();
        itemHolder.tvPerson.setText(person.getName());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class GroupHolder {
        public TextView txt;
        public CheckBox cb;
    }

    class ItemHolder {
        public CheckBox cb;
        public TextView tvTrouble;
        public TextView tvOperator;
        public TextView tvPart;
        public ImageView ivMinus;
        public TextView tvCount;
        public ImageView ivAdd;
        public TextView tvUnit;
        public TextView tvPerson;
        public ImageView btnDelete;
        public ImageView btnComplete;
    }

}
